package com.b2s.scrumblr.kb.base;

import com.b2s.scrumblr.kb.exception.ScrumblrFileException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2016/8/15.
 */
@Component
public class BootstrapHtmlTemplate extends HtmlTemplateBase {

    public BootstrapHtmlTemplate(){}

    public BootstrapHtmlTemplate(String header, String body) {
        super(header, body);
    }

    public BootstrapHtmlTemplate(String header, String body, boolean useHeaderTemplate, boolean useBodyTemplate) {
        super(header, body, useHeaderTemplate, useBodyTemplate);
    }

    @Override
    public String header() {

        if(isUseHeaderTemplate()){
            String filePath = BootstrapHtmlTemplate.class.getClassLoader().getResource("template/bootstrap-header.html").getFile();
            System.out.println(filePath);
            try {

                String headerTemplate = FileUtils.readFileToString(new File(filePath));
                String header = getHeader();
                if(StringUtils.isBlank(getHeader())) header = "";
                return headerTemplate.replace("${header}", header);
            } catch (IOException e) {
                throw new ScrumblrFileException("Can not found the file:", e);
            }
        }else{
            return getHeader();
        }
    }

    @Override
    public String body() {
        if(isUseBodyTemplate()){
            StringBuilder sb = new StringBuilder();

            sb.append("<body>").append("<div class='container'>");
            if(StringUtils.isNotBlank(getBody())) sb.append(getBody());
            sb.append("</div>").append("</body>");

            return sb.toString();
        }else{
            return getBody();
        }
    }

}
